using SqlSugar;
using Hospital.WebApi;
using Microsoft.OpenApi.Models;
using Hospital.IRepository;
using Hospital.Repository;
using Hospital.IService;
using Hospital.Service;
using Hospital.Utility.Mapper;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();


builder.Services.AddControllers(options =>
{
    options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true;
});

#region SqlSugar注入
builder.Services.AddSingleton<ISqlSugarClient>(s =>
{
    SqlSugarScope sqlSugar = new SqlSugarScope(new ConnectionConfig()
    {
        DbType = DbType.SqlServer,
        ConnectionString = "Server=.;Database=Hospital;uid=sa;pwd=Betamio2003;Encrypt=True;TrustServerCertificate=True;",
        IsAutoCloseConnection = true,
        InitKeyType = InitKeyType.Attribute
    });
    return sqlSugar;
});
#endregion
#region IOC注入
//builder.Services.AddScoped<IBlogNewsService, BlogNewService>();
//builder.Services.AddScoped<ITypeInfoService, TypeInfoService>();
//builder.Services.AddScoped<IUserInfoService, UserInfoService>();
//builder.Services.AddScoped<IBlogNewRepository, BlogNewRepository>();
//builder.Services.AddScoped<ITypeInfoRepository, TypeInfoRepository>();
//builder.Services.AddScoped<IUserInfoRepository, UserInfoRepository>();
//builder.Services.AddScoped<IPatientRepository, PatientRepository>();
//builder.Services.AddScoped<IPatientService, PatientService>();
//builder.Services.AddScoped<IDoctorRepository, DoctorRepository>();
//builder.Services.AddScoped<IDoctorService, DoctorService>();
builder.Services.AddApplicationServices();
#endregion
#region JWT鉴权
builder.Services.AddCustomJWT();
#endregion

#region JWT鉴权使用,此时会让你填入一个token
builder.Services.AddSwaggerGen(setupAction =>
{
    setupAction.SwaggerDoc("v1", new OpenApiInfo { Title = "Hospital.WebApi", Version = "v1" });
    //使用组件
    setupAction.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
    {
        In = ParameterLocation.Header,
        Type = SecuritySchemeType.ApiKey,
        Description = "输入Bearer {token}",
        Name = "Authorization",
        BearerFormat = "JWT",
        Scheme = "Bearer"
    });
    setupAction.AddSecurityRequirement(new OpenApiSecurityRequirement
    {
        {
            new OpenApiSecurityScheme
            {
                Reference = new OpenApiReference
                {
                    Type = ReferenceType.SecurityScheme,
                    Id = "Bearer"
                }
            },
            new string[]{}
        }
    });
});
#endregion
#region 注入AutoMapper
builder.Services.AddAutoMapper(typeof(CustomAutoMapperProfile));
#endregion
#region 跨域
// 添加 CORS 策略
builder.Services.AddCors(options =>
{
    options.AddPolicy("AllowAll", policy =>
    {
        policy.AllowAnyOrigin()    // 允许所有来源（生产环境应指定具体域名！）
              .AllowAnyMethod()    // 允许所有 HTTP 方法
              .AllowAnyHeader();   // 允许所有请求头
    });
});
#endregion
var app = builder.Build();
app.UseCors("AllowAll");
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}
app.UseHttpsRedirection();
//使用身份验证,鉴权
app.UseAuthentication();
//授权
app.UseAuthorization();
app.MapControllers();
app.Run();




